package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.BdmGood;
import com.ruoyi.system.domain.dto.BdmShippingCostDto;
import com.ruoyi.system.domain.dto.BdmShoppingCostDto;
import com.ruoyi.system.mapper.BdmGoodMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BdmShippingCostMapper;
import com.ruoyi.system.domain.BdmShippingCost;
import com.ruoyi.system.service.IBdmShippingCostService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运费Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-03
 */
@Service
public class BdmShippingCostServiceImpl implements IBdmShippingCostService 
{
    @Autowired
    private BdmShippingCostMapper bdmShippingCostMapper;

    @Autowired
    private BdmGoodMapper bdmGoodMapper;

    /**
     * 查询运费
     * 
     * @param id 运费主键
     * @return 运费
     */
    @Override
    public BdmShippingCost selectBdmShippingCostById(Long id)
    {
        return bdmShippingCostMapper.selectBdmShippingCostById(id);
    }

    /**
     * 查询运费列表
     * 
     * @param bdmShippingCost 运费
     * @return 运费
     */
    @Override
    public List<BdmShippingCost> selectBdmShippingCostList(BdmShippingCost bdmShippingCost)
    {
        return bdmShippingCostMapper.selectBdmShippingCostList(bdmShippingCost);
    }

    /**
     * 新增运费
     * 
     * @param bdmShippingCost 运费
     * @return 结果
     */
    @Override
    public int insertBdmShippingCost(BdmShippingCost bdmShippingCost)
    {
        bdmShippingCost.setCreateTime(DateUtils.getNowDate());
        return bdmShippingCostMapper.insertBdmShippingCost(bdmShippingCost);
    }

    /**
     * 修改运费
     * 
     * @param bdmShippingCost 运费
     * @return 结果
     */
    @Override
    public int updateBdmShippingCost(BdmShippingCost bdmShippingCost)
    {
        bdmShippingCost.setUpdateTime(DateUtils.getNowDate());
        return bdmShippingCostMapper.updateBdmShippingCost(bdmShippingCost);
    }

    /**
     * 批量删除运费
     * 
     * @param ids 需要删除的运费主键
     * @return 结果
     */
    @Override
    public int deleteBdmShippingCostByIds(Long[] ids)
    {
        return bdmShippingCostMapper.deleteBdmShippingCostByIds(ids);
    }

    /**
     * 删除运费信息
     * 
     * @param id 运费主键
     * @return 结果
     */
    @Override
    public int deleteBdmShippingCostById(Long id)
    {
        return bdmShippingCostMapper.deleteBdmShippingCostById(id);
    }

    @Override
    @Transactional
    public AjaxResult saveCost(BdmShoppingCostDto bdmShoppingCostDto) {
        bdmShippingCostMapper.deleteBdmShippingCostByType(bdmShoppingCostDto.getType());
        for (BdmShoppingCostDto.Rule rule : bdmShoppingCostDto.getRules()) {
            BdmShippingCost bdmShippingCost = new BdmShippingCost();
            bdmShippingCost.setStartPrice(rule.getStartPrice());
            bdmShippingCost.setEndPrice(rule.getEndPrice());
            bdmShippingCost.setPrice(rule.getPrice());
            bdmShippingCost.setType(bdmShoppingCostDto.getType());
            bdmShippingCostMapper.insertBdmShippingCost(bdmShippingCost);
        }
        return AjaxResult.success();
    }

    @Override
    public List<BdmShippingCost> selectBdmShippingCostListDto(BdmShippingCostDto bdmShippingCostDto) {
        Long goodId = bdmShippingCostDto.getGoodId();
        BdmGood bdmGood = bdmGoodMapper.selectbdmGoodById(goodId);
        if (bdmGood != null && bdmGood.getClassOneId() == 2) {
            BdmShippingCost bdmShippingCost = new BdmShippingCost();
            BeanUtils.copyProperties(bdmShippingCostDto, bdmShippingCost);
            return bdmShippingCostMapper.selectBdmShippingCostList(bdmShippingCost);
        } else {
            return List.of();
        }
    }
}
